package cc.owoo.godpen.analysis.css;

import cc.owoo.godpen.analysis.html.Element;
import cc.owoo.godpen.analysis.html.ElementList;

import java.util.Queue;

/**
 * 标签选择器
 * Created by nimensei
 * 2022-06-02 上午 12:15
 */
public class TagSelector extends SelectorNode {
    private final String tagName;// 标签名称

    /**
     * 创建一个标签选择器节点
     *
     * @param tagName class名称，不能为空和为null
     */
    public TagSelector(String tagName) {
        if (tagName == null || (tagName = tagName.trim().toLowerCase()).length() == 0)
            throw new NullPointerException("className不能为空");
        this.tagName = tagName;
    }

    @Override
    void selectElement(Element element, Queue<Element> elements) {
        ElementList elementList = element.getElementsByTagName(tagName);
        if (elementList.size() == 0)
            return;
        for (Element child : elementList)
            elements.add(child);
    }

    @Override
    void matchingElement(Element element, Queue<Element> elements) {
        String elementTagName = element.getTagName();
        if (elementTagName == null || !elementTagName.equals(tagName))
            return;
        elements.add(element);
    }

    @Override
    public void stringify(StringBuilder string) {
        string.append(tagName);
    }
}
